package org.springframework.core.type.classreading;

import java.util.ArrayList;
import java.util.List;

import org.springframework.asm.MethodVisitor;
import org.springframework.core.type.ServiceMethodMetadata;

/**
 * 在service接口上，不需要使用任何的注解
 * @author jackson
 *
 */
public class ServiceClassMetadataReadingVisitor extends ProxyClassMetadataReadingVisitor{
	protected final List<ServiceMethodMetadata> methods;
	protected final List<ServiceClassMetadataReadingVisitor> extendInterfaces;
	/**
	 * 表示这个接口由哪一个类或虚类来实现
	 */
	protected ImplementClassMetadataReadingVisitor implementClass;
	
	public ServiceClassMetadataReadingVisitor(ClassLoader classLoader) {
		super(classLoader);
		this.methods = new ArrayList<ServiceMethodMetadata>();
		this.extendInterfaces = new ArrayList<ServiceClassMetadataReadingVisitor>();
	}
	
	public ImplementClassMetadataReadingVisitor getImplementClass() {
		return implementClass;
	}
	
	public void setImplementClass(ImplementClassMetadataReadingVisitor implementClass) {
		this.implementClass = implementClass;
	}
	
	public void add(ServiceClassMetadataReadingVisitor scm){
		extendInterfaces.add(scm);
	}
//	public Collection<ServiceMethodMetadata> getMethods(){
//		List<ServiceMethodMetadata> ms = new ArrayList<ServiceMethodMetadata>(methods);
//		for(ServiceClassMetadataReadingVisitor scmr : extendInterfaces){
//			ms.addAll(scmr.getMethods());
//		}
//		return ms;
//	}
//	
	public void fillMethods(List<ServiceMethodMetadata> methods){
		methods.addAll(this.methods);
	}

	@Override
	public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
		if(!isInterface()) return null;//如果不是接口，则放弃处理
		MethodVisitor mv = proxy.visitMethod(access, name, desc, signature, exceptions);
		ServiceMethodMetadataReadingVisitor smmrv = new ServiceMethodMetadataReadingVisitor(this, access, name, desc, signature, exceptions, mv);
		methods.add(smmrv);
		return smmrv;
	}

}
